<template>
  <div class='container'>
    <el-dialog :title="dialogTitle" :visible="dialogVisible" :close-on-click-modal="false" @close="closeDialogFn" width="400px" >
      <el-form ref="ruleForm" :model="formData" :rules="rules">
        <el-form-item label="学科名称" prop="subjectName" label-width="80px">
          <el-input v-model="formData.subjectName" placeholder="请输入学科名称"></el-input>
        </el-form-item>
        <el-form-item label="是否启用" prop="isFrontDisplay">
          <el-switch
            v-model="formData.isFrontDisplay"
            :active-value="1"
            :inactive-value="0">
          </el-switch>
        </el-form-item>
      </el-form>
      <!-- footer插槽 -->
      <template v-slot:footer>
        <el-button @click="closeDialogFn">取消</el-button>
        <el-button type="primary" @click="confirmAddSubject">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { add, update } from '@/api/hmmm/subjects'

export default {
  name: 'SubjectsAdd',
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    },
    dialogTitle: {
      type: String,
      default: '新增学科'
    },
    formData: {
      type: Object,
      required: true
    }
  },
  data () {
    return {
      rules: {
        subjectName: [{ required: true, message: '权限名称', trigger: 'blur' }]
      }
    }
  },
  methods: {
    /** 关闭dialog */
    closeDialogFn () {
      this.$emit('update:dialogVisible', false)
      this.$refs.ruleForm.resetFields()
      this.$emit('update:formData', {})
    },
    /** 点击确定提交请求 */
    confirmAddSubject () {
      this.$refs.ruleForm.validate(async flag => {
        if (!flag) return
        if (this.formData.id) {
          await update(this.formData)
          this.$message.success('修改学科成功')
        } else {
          await add(this.formData)
          this.$message.success('新增学科成功')
        }
        this.closeDialogFn()
        this.$parent.subjectsListApi()
      })
    }
  }
}
</script>

<style lang="scss" scoped></style>
